package com.grace.controller;

import com.grace.common.ApplicationContextHelper;
import com.grace.common.JsonData;
import com.grace.dao.SysAclModuleMapper;
import com.grace.exception.ParamException;
import com.grace.exception.PermissionException;
import com.grace.model.SysAclModule;
import com.grace.param.TestVo;
import com.grace.util.BeanValidator;
import com.grace.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {
    @RequestMapping("/hello")
    @ResponseBody
    public String test() {
        /*日志*/
        log.info("hello");
        return "hello, SSM";
    }

    @RequestMapping("/hello.json")
    @ResponseBody
    public JsonData hello1() {
        log.info("hello");
        //throw new RuntimeException("test exception");
        return JsonData.success("hello,permission");
    }

/*    @RequestMapping("/hello2.json")
    @ResponseBody
    public JsonData hello2() {
        log.info("hello");
        throw new PermissionException("test exception");
        //return JsonData.success("hello,permission");
    }

    @RequestMapping("/hello3.json")
    @ResponseBody
    public JsonData hello3() {
        log.info("hello");
        throw new RuntimeException("test exception");
        //return JsonData.success("hello,permission");
    }*/

    @RequestMapping("/permission.json")
    @ResponseBody
    public JsonData test2() {
        log.info("permission.json");
        throw new PermissionException("test");
    }

    @RequestMapping("/permission.page")
    @ResponseBody
    public JsonData test3() {
        log.info("permission.page");
        throw new PermissionException("test");
    }


    @RequestMapping("/validate2.json")
    @ResponseBody
    public JsonData test4(TestVo vo) throws ParamException {
        log.info("validate");
        try {
            //创建map接收validator传过来的校验结果
            Map<String, String> map = BeanValidator.validatorObject(vo);
            //if (map != null && map.entrySet().size() > 0) { //说明map有值
            if (MapUtils.isNotEmpty(map)) { //说明map有值
                //增强for循环
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    log.info("{} -> {}", entry.getKey(), entry.getValue());//使用占位符的方式
                }
            }
        } catch (Exception ex) {

        }

        return JsonData.success( "test validate");
    }


    @RequestMapping("/validate.json")
    @ResponseBody
    public JsonData validate(TestVo vo) throws ParamException {
        log.info("validate");
        BeanValidator.check(vo); //通过ParamException抛出异常给前端，请求不合法
        return JsonData.success( "test validate");
    }
    @RequestMapping("/validate1.json")
    @ResponseBody
    public JsonData test5(TestVo vo) throws ParamException {
        log.info("validate1");
        //检查上下文
        SysAclModuleMapper moduleMapper = ApplicationContextHelper.popBean(SysAclModuleMapper.class);
        //查询id为1的权限模块
        SysAclModule module = moduleMapper.selectByPrimaryKey(1);
        //id为1的权限模块输出到日志控制台
        log.info(JsonMapper.obj2String(module));
        //验证参数
        BeanValidator.check(vo);

        return JsonData.success("test", "validate1");
    }


}
